<template>
  <div>
    <div class="top">
      <el-input
        v-model="model.jName"
        @change="getPlanList"
        style="width: 200px"
        placeholder="请输入作业计划名称"
        prefix-icon="el-icon-search"
      >
      </el-input>
      <div>
        <el-button size="small" @click="toAplan" type="primary"
          >布置作业</el-button
        >
      </div>
    </div>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      element-loading-text="Loading"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }"
      :row-class-name="xxxTableRowClassName"
    >
      <!--      row-click 行点击事件-->
      <el-table-column
        align="center"
        prop="jName"
        label="作业计划名称"
      ></el-table-column>
      <el-table-column align="center" label="计划状态">
        <template slot-scope="scope">
          <el-switch
            @change="changeState(scope.row)"
            :active-value="2"
            :inactive-value="1"
            v-model="scope.row.jStatus"
            active-color="#13ce66"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="AI评价">
        <template slot-scope="scope">
          <el-switch
            @change="changeState(scope.row)"
            :active-value="1"
            :inactive-value="2"
            v-model="scope.row.jAutomatic"
            active-color="#13ce66"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            plain
            @click="lookMessage($event, scope.row)"
            >查看</el-button
          >
          <el-button
            style="margin-left: 15px"
            size="small"
            plain
            type="danger"
            @click="decrementMessage($event, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination-page :total="total" @size="changeSize" @page="changePage" />
  </div>
</template>

<script>
import { PlanList, changePlanStatus, decrementPlan } from "@/api/sports/plan";
export default {
  name: "index",
  data() {
    return {
      key: "",
      tableData: [],
      value: true,
      model: {
        pageNum: 1,
        pageSize: 10,
        jName: "",
      },
      total: -1,
      isLoading: false,
    };
  },
  created() {
    this.getPlanList();
  },
  methods: {
    xxxTableRowClassName({ row, rowIndex }) {
      this.dialogFormVisible = true;
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    lookMessage(evt, e) {
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.$router.push({
        path: "/sports/planAdd",
        query: {
          id: e.jId,
          type: 1,
        },
      });
    },
    decrementMessage(evt, e) {
      let target = evt.target;
      const { jId } = e;
      this.$confirm("此操作将永久删除该作业计划, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          decrementPlan({ jId, jeSource: 5 }).then((response) => {
            if (response.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getPlanList();
            } else {
              this.$message({
                type: "error",
                message: response.message,
              });
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    toAplan() {
      this.$router.push({
        path: "/sports/Aplan",
      });
    },
    changeSize(e) {
      this.model.pageSize = e;
      this.getPlanList();
    },
    changePage(e) {
      this.model.pageNum = e;
      this.getPlanList();
    },
    async getPlanList() {
      this.isLoading = true;
      const result = await PlanList({
        ...this.model,
        jShId: this.$store.state.user.USER_INFO.pscShId,
        uId: this.$store.state.user.USER_INFO.id,
      });
      if (result.code === 200) {
        this.tableData = result.result.list;
        this.total = result.result.total;
        this.isLoading = false;
      }
    },
    changeState(e) {
      this.isLoading = true;
      const { jId, jStatus, jAutomatic } = e;
      changePlanStatus({ jId, jStatus, jAutomatic }).then((response) => {
        if (response.code === 200) {
          this.isLoading = false;
          this.$message.success(response.message);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
}
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
</style>
